home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Get1IndString
/
Get1IndString.sit
/
Get1IndString.cp
next >
Wrap
Text File
|
1995-06-28
|
3KB
|
117 lines
// Get1IndString.cp -- get a specific string from a STR# resource
// this is the same as the Toolbox function GetIndString except only the
// first (current) resource file is searched.
//
// Also includes utility functions for counting the number of strings in
// an STR# resource.
//
// Use the C++ compiler to compile this code. You may call the functions from
// either C or C++.
//
// Copyright ⌐ 1995 Gregory W. Jorgensen, All Rights Reserved
// May be freely distributed and used if this copyright notice is retained
//
// NOTE: this function does not change the state of the STR# resource,
// nor does it release or purge the STR# resource when done. If you don't
// want the STR# resource to sit around in the heap mark it purgeable
// in the resource attributes (with ResEdit or Resorcerer), or do this:
//
// Handle h = Get1Resource('STR#', resid);
// if ( h ) HPurge(h);
#include "Get1IndString.h"
// this struct maps the STR# resource structure, sort of
struct strlist {
short count;
unsigned char data[1];
};
// Get1IndString -- get a specified string from a STR# resource
// if the resource is not found, or the specified string is not in the STR#
// resource, the string is returned empty (0 in the first byte)
void Get1IndString(Str255 thestring, short resid, short index)
{
thestring[0] = 0;
// get the resource
Handle h = Get1Resource('STR#', resid);
if ( h ) {
// map handle to strlist structure
strlist* sp = (strlist*)(*h);
// if index is in range, scan through stringlist for requested string
if ( index > 0 && index <= sp->count ) {
unsigned char* s;
for ( s = sp->data; --index; s += (s[0] + 1) ) continue;
// copy the found string into the Str255 parameter
// the extra byte copied because of the <= comparison is the length
for ( short i = 0; i <= s[0]; ++i )
thestring[i] = s[i];
}
}
}
// get1indstring -- C glue for Get1IndString
// same thing as Get1IndString except returns a C string
// the passed string must be a char array of at least 256 bytes
// (no wimpy C glue that just calls the Pascal function here)
char* get1indstring(char* thestring, short resid, short index)
{
if ( thestring ) {
thestring[0] = 0;
// get the resource
Handle h = Get1Resource('STR#', resid);
if ( h ) {
// map handle to strlist structure
strlist* sp = (strlist*)(*h);
// if index is in range, scan through stringlist for requested string
if ( index > 0 && index <= sp->count ) {
unsigned char* s;
for ( s = sp->data; --index; s += (s[0] + 1) ) continue;
// copy the found string into the Str255 parameter
for ( short i = 0; i < s[0]; ++i )
thestring[i] = s[i+1];
// terminate the C string
thestring[ s[0] ] = 0;
}
}
}
return thestring;
}
// CountStrings -- return number of strings in an STR# resource
short CountStrings(short resid)
{
short count = 0;
Handle h = GetResource('STR#', resid);
if ( h ) {
strlist* sp = (strlist*)(*h);
count = sp->count;
}
return count;
}
// Count1Strings -- return number of strings in an STR# resource (1-deep search)
short Count1Strings(short resid)
{
short count = 0;
Handle h = Get1Resource('STR#', resid);
if ( h ) {
strlist* sp = (strlist*)(*h);
count = sp->count;
}
return count;
}